% Import allocations for a given number of problems

function calcGarp(n)

format short

DA = './dalloc';
RA = './ralloc';
DA2 = './p2dalloc';
RA2 = './p2ralloc';
RA1 = './p1ralloc';

filename = [[DA], num2str(n), '.csv'];
md = importdata(filename);
md = sortrows(md,1);

filename = [[RA], num2str(n), '.csv'];
mr = importdata(filename);
mr = sortrows(mr,1);

filename = [[DA2], num2str(n), '.csv'];
md2 = importdata(filename);
md2 = sortrows(md2,1);

filename = [[RA2], num2str(n), '.csv'];
mr2 = importdata(filename);
mr2 = sortrows(mr2,1);

filename = [[RA1], num2str(n), '.csv'];
mr1 = importdata(filename);
mr1 = sortrows(mr1,1);

mc = [md;mr];
mc = sortrows(mc,1);

mc2 = [mr1;mr2;md2];
mc2 = sortrows(mc2,1);
mc2;

size(mc2)

md(1:13,1:end);
mr(1:13,1:end);

user = md(:,1);
userc = mc(:,1);
nprobsc = mc(:,2);
ncompanies = (size(md,2) - 2)/2;
p = md(:,3+n:end);
pc = mc(:,3+n:end);
pc2 = mc2(:,3+n:end);
xd = md(:,3:3+ncompanies-1);
xr = mr(:,3:3+ncompanies-1);
xc = mc(:,3:3+ncompanies-1);
xc2 = mc2(:,3:3+ncompanies-1);
nusers = size(xd,1)/10;
userp = mc2(:,1);

ncompanies;
nusers;
xd(1:13,1:end);
xr(1:13,1:end);
xc(1:13,1:end);

[Usersd Afriatd LawOfDemandd sarpd flagd vpairsd] = testGarp(user,xd,p, 10); 
[Usersc Afriatc LawOfDemandc sarpc flagc vpairsc] = testGarp(userc,xc,pc, 20);
[Usersr Afriatr LawOfDemandr sarpr flagr vpairsr] = testGarp(user,xr,p, 10);
[Usersp Afriatp2 LawOfDemandc sarpc flagp2 vpairsp2] = testGarp(userp,xc2,pc2, 20);

[Usersd, Usersr, Usersc];               
[Afriatr, Afriatd, Afriatc, Afriatd-Afriatc];

res = [Usersd Afriatr Afriatd Afriatc flagr flagd flagc vpairsr vpairsd vpairsc];
filename = ['Afriat', num2str(n), '.txt'];
dlmwrite(filename, res, 'delimiter', ',', 'precision', 6, 'newline', 'pc');

res = [Usersp Afriatp2 flagp2 vpairsp2];
filename = ['Afriatp2', num2str(n), '.txt'];
dlmwrite(filename, res, 'delimiter', ',', 'precision', 6, 'newline', 'pc');

md(1:21,1:end);
mr(1:21,1:end);
mc(1:21,1:end);
